const express = require("express");
const router = express.Router();
const multer = require("multer");
const path = require("path");
const storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, path.join(__dirname, "../../public/uploads"));
    },
    filename: function (req, file, cb) {
        const lastName = Math.random().toString(36).substring(2, 8)
        const extname = path.extname(file.originalname);
        cb(null, Date.now() + "-" + lastName + extname);
    }
})

const upload = multer({ storage: storage });

router.post("/", upload.single("img"), (req, res) => {
    res.send(req.file);
});

module.exports = router;
